# pip install pyecharts==2.0.8

from pyecharts.charts import Sankey
from pyecharts import options as opts

# 定义节点和链接
nodes = [

    {"name": "居民增加值"},
    {"name": "企业增加值"},
    {"name": "政府增加值"},
    {"name": "金融机构增加值"},

    {"name": "居民初次分配总收入"},
    {"name": "政府初次分配总收入"},
    {"name": "金融机构初次分配总收入"},
    {"name": "企业初次分配总收入"},
    {"name": "劳动者报酬"},
    {"name": "生产税净额"},
    {"name": "财产收入"},
    
    {"name":"经常转移"},
    {"name":"实物转移"},
    
    {"name": "居民可支配总收入"},
    {"name": "政府可支配总收入"},
    {"name": "金融机构可支配总收入"},
    {"name": "企业可支配总收入"},
    
    {"name":"调整后政府可支配总收入"},
    {"name":"调整后居民可支配总收入"},
    {"name":"调整后企业可支配总收入"},
    {"name":"调整后金融机构可支配总收入"},
    
    {"name":"居民最终消费支出"},
    {"name":"政府最终消费支出"},
    {"name":"最终消费支出"},
    
    {"name":"企业总储蓄"},
    {"name":"政府总储蓄"},
    {"name":"金融机构总储蓄"},
    {"name":"居民总储蓄"},
    
    {"name":"金融市场"},
    {"name":"存量非金融资产"},
    {"name":"资本转移"},
    {"name":"国外金融资产"},
    
    {"name":"企业投资账户"},
    {"name":"政府投资账户"},
    {"name":"金融机构投资账户"},
    {"name":"居民投资账户"},
    
    {"name":"资本形成"}



]
links = [

    {"source": "企业增加值", "target": "劳动者报酬", "value": 343518},
    {"source": "政府增加值", "target": "劳动者报酬", "value": 107396},
    {"source": "金融机构增加值", "target": "劳动者报酬", "value": 24562},
    {"source": "居民增加值", "target": "劳动者报酬", "value": 158615},
    {"source": "居民增加值", "target": "生产税净额", "value": -396},
    {"source": "企业增加值", "target": "生产税净额", "value": 93601},
    {"source": "政府增加值", "target": "生产税净额", "value": 330},
    {"source": "金融机构增加值", "target": "生产税净额", "value": 9998},
    {"source": "居民增加值", "target": "财产收入", "value": 17609},
    {"source": "企业增加值", "target": "财产收入", "value": 93200},
    {"source": "政府增加值", "target": "财产收入", "value": 14593},
    {"source": "金融机构增加值", "target": "财产收入", "value": 96044},
    {"source":"劳动者报酬","target":"居民初次分配总收入","value": 634273},
    # 财产收入去处
    {"source":"财产收入","target":"居民初次分配总收入","value": 56410},
    {"source":"财产收入","target":"企业初次分配总收入","value": 35659},
    {"source": "财产收入", "target": "金融机构初次分配总收入", "value": 87485},
    {"source": "财产收入", "target": "政府初次分配总收入", "value": 31388},
    
    {"source": "居民增加值", "target": "居民初次分配总收入", "value": 52200},
    # 生产税净额去处
    {"source":"生产税净额","target":"政府初次分配总收入", "value":103533},
    {"source": "金融机构增加值", "target": "金融机构初次分配总收入", "value": -37319},
    {"source": "政府增加值", "target": "政府初次分配总收入", "value": -939},
    {"source": "企业增加值", "target": "企业初次分配总收入", "value": 231712},
    # 经常转移
    {"source": "居民初次分配总收入", "target": "经常转移", "value": 107322},
    {"source": "经常转移", "target": "居民可支配总收入", "value": 90981, 'color':'red'},
    {"source": "居民初次分配总收入", "target": "居民可支配总收入", "value": 635560},
    
    {"source": "政府初次分配总收入", "target": "经常转移", "value": 82849},
    {"source": "经常转移", "target": "政府可支配总收入", "value": 147910},
    {"source": "政府初次分配总收入", "target": "政府可支配总收入", "value": 51132},
    
    {"source": "企业初次分配总收入", "target": "经常转移", "value": 41423, 'color':'red'},
    {"source": "经常转移", "target": "企业可支配总收入", "value": 3114},
    {"source": "企业初次分配总收入", "target": "企业可支配总收入", "value": 225948, 'color':'green'},
    
    {"source": "金融机构初次分配总收入", "target": "经常转移", "value": 17065},
    {"source": "经常转移", "target": "金融机构可支配总收入", "value": 8011},
    {"source": "金融机构初次分配总收入", "target": "金融机构可支配总收入", "value": 33100},
    
    {"source": "实物转移", "target": "调整后居民可支配总收入", "value": 79884},
    {"source": "居民可支配总收入", "target": "调整后居民可支配总收入", "value": 726541},

    {"source": "政府可支配总收入", "target": "实物转移", "value": 79884},
    {"source": "政府可支配总收入", "target": "调整后政府可支配总收入", "value": 119159},
    
    {"source": "企业可支配总收入", "target": "调整后企业可支配总收入", "value": 229063},
    # 总储蓄
    {"source": "调整后企业可支配总收入", "target": "企业总储蓄", "value": 229063},
    {"source": "调整后政府可支配总收入", "target": "政府总储蓄", "value": 5683},
    {"source": "调整后居民可支配总收入", "target": "居民总储蓄", "value": 276073},
    {"source": "金融机构可支配总收入", "target": "调整后金融机构可支配总收入", "value": 41111},
    {"source": "调整后金融机构可支配总收入", "target": "金融机构总储蓄", "value": 41111},
    # 最终消费
    {"source": "调整后政府可支配总收入", "target": "政府最终消费支出", "value": 113476},
    {"source": "调整后居民可支配总收入", "target": "居民最终消费支出", "value": 530352},
    {"source": "政府最终消费支出", "target": "最终消费支出", "value": 113476},
    {"source": "居民最终消费支出", "target": "最终消费支出", "value": 530352},
    # 金融市场
    {"source": "居民总储蓄", "target": "金融市场", "value": 184653},
    {"source": "金融机构总储蓄", "target": "金融市场", "value": 38559},
    # 投资账户
    {"source": "金融机构总储蓄", "target": "金融机构投资账户", "value": 2552},
    {"source":"居民总储蓄", "target": "居民投资账户", "value": 91419},
    {"source": "金融市场", "target": "企业投资账户", "value": 155833},
    {"source": "金融市场", "target": "政府投资账户", "value": 35263},
    {"source": "金融市场", "target": "国外金融资产", "value": 29994},
    # 资本转移
    {"source": "政府总储蓄", "target": "资本转移", "value": 10336},
    {"source": "资本转移", "target": "企业投资账户", "value": 10299},
    # 企业
    {"source": "企业总储蓄", "target": "存量非金融资产", "value": 56966},
    {"source": "企业总储蓄", "target": "企业投资账户", "value": 172096},
    # 投资去向
    {"source":"存量非金融资产", "target":"政府投资账户", "value":32891},
    {"source":"存量非金融资产", "target":"居民投资账户", "value":24075},
    {"source": "企业投资账户", "target": "资本形成", "value": 338229},
    {"source": "政府投资账户", "target": "资本形成", "value": 63518},
    {"source": "居民投资账户", "target": "资本形成", "value": 115494},
    {"source": "金融机构投资账户", "target": "资本形成", "value": 2552},
]

# 创建桑基图
sankey = Sankey(init_opts=opts.InitOpts(width="1900px", height="800px"))
sankey.add(
    "",
    nodes,
    links,
    linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5),
    label_opts=opts.LabelOpts(position="right")
)
sankey.set_global_opts(
    title_opts=opts.TitleOpts(title="2022年资金流量表桑基图"),
                       
                )
sankey.render("中国2022年资金流量表.html")